package com.i3q360.layui.modular.system.web;


import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.i3q360.layui.common.base.BaseController;
import com.i3q360.layui.core.shiro.ShiroKit;
import com.i3q360.layui.modular.system.entity.SysRole;

@Controller
public class HomeController extends BaseController{
	
	
    @RequestMapping({"/","/index"})
    public String index(Model model){
    	
    	List<SysRole> list = ShiroKit.getUser().getRoleList();
    	if(list == null || list.size() == 0){
    		ShiroKit.getSubject().logout();
    		return PREFIX + "/login.html";
    	}
    	model.addAttribute("userInfo", ShiroKit.getUser());
        return PREFIX + "/index.html";
    }
   
    @RequestMapping(value="/login",method=RequestMethod.GET)
    public String loginGet(Model model){
    	if( ShiroKit.isAuthenticated() || ShiroKit.getUser() != null ){
    		return REDIRECT+"/";
    	}else
    		return PREFIX+"login.html";
    }
     ////use ajax post ===========> see CustomFormAuthenticationFilter
//    @RequestMapping(value="/login",method=RequestMethod.POST)
//    public String login(HttpServletRequest request, Model model) throws Exception{
//        System.out.println("HomeController.login()");
//        // 登录失败从request中获取shiro处理的异常信息。
//        // shiroLoginFailure:就是shiro异常类的全类名.
//        String exception = (String) request.getAttribute("shiroLoginFailure");
//        System.out.println("exception=" + exception);
//        String msg = "";
//        if (exception != null) {
//            if (UnknownAccountException.class.getName().equals(exception)) {
//                System.out.println("UnknownAccountException -- > 账号不存在：");
//                msg = "UnknownAccountException -- > 账号不存在：";
//            } else if (IncorrectCredentialsException.class.getName().equals(exception)) {
//                System.out.println("IncorrectCredentialsException -- > 密码不正确：");
//                msg = "IncorrectCredentialsException -- > 密码不正确：";
//            } else if ("kaptchaValidateFailed".equals(exception)) {
//                System.out.println("kaptchaValidateFailed -- > 验证码错误");
//                msg = "kaptchaValidateFailed -- > 验证码错误";
//            } else {
//                msg = "else >> "+exception;
//                System.out.println("else -- >" + exception);
//            }
//        }
//        model.addAttribute("msg", msg);
//   //      此方法不处理登录成功,由shiro进行处理
//        return PREFIX+"login.html";
//    }

    @RequestMapping("/403")
    public String unauthorizedRole(){
        System.out.println("------没有权限-------");
        return "403.html";
    }

    @RequestMapping("/logout")
    public String logout(){
    
    	ShiroKit.getSubject().logout();
    	
    	return PREFIX + "/login";
    }
    
}